package gui.panel;

import javax.swing.*;
import java.awt.*;

public class CenterPanel extends JPanel{
    private double rate;//拉伸比例
    private JComponent c;//显示的组件
    private boolean strech;//是否拉伸

    public CenterPanel(double rate,boolean strech){
        this.setLayout(null);
        this.rate = rate;
        this.strech = strech;
    }

    public CenterPanel(double rate){
        this(rate,true);
    }

    public void repaint(){
        if(null!=c){
            Dimension containerSize = this.getSize();
            Dimension componentSize = c.getPreferredSize();

            if(strech){
                c.setSize((int)(containerSize.width*rate),(int)(containerSize.height*rate));
            }else{
                c.setSize(componentSize);
            }

            c.setLocation(containerSize.width/2-c.getSize().width/2,containerSize.height/2-c.getSize().height/2);
        }
        super.repaint();
    }

    public void show(JComponent p){
        this.c = p;
        Component[] cs = getComponents();
        for(Component c : cs){
            remove(c);
        }
        add(p);
        this.updateUI();
    }

    public static void main(String[] args){
        JFrame j = new JFrame();
        j.setSize(200,200);
        j.setLocationRelativeTo(null);
        CenterPanel cp = new CenterPanel(0.85,true);
        j.setContentPane(cp);
        j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        j.setVisible(true);
        JButton jb = new JButton("abc");
        cp.show(jb);
    }
}
